/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.latticeware.xtalk.app;

import com.latticeware.xtalk.stack.AnswerDialog;
import com.latticeware.xtalk.stack.AskDialog;
import com.latticeware.xtalk.stack.Stack;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;



/**
 *
 * @author sfisque
 */
public class LatticeCard
{
    private static LatticeCard latticeCard;
    
    protected Frame window;
    
    protected ToolPalette toolPalette;
    
    protected Stack stack;
    
    protected AskDialog askDialog;
    
    protected AnswerDialog answerDialog;
    
    
    
    private LatticeCard() throws InterruptedException
    {
        window = new Frame( "Lattice Card" );
        window.setResizable( false );
        window.setSize( 512, 384 );
        window.setLocation( 120, 20 );
        window.addWindowListener( new LatticeWindowListener() );
        window.setLayout( new GridLayout( 1, 1 ) );
        
        window.setMenuBar( createMenuBar() );
        
        toolPalette = new ToolPalette( window );
        stack = new Stack();
        window.add( stack.getCurrentCard().getVisualEntity() );
    }
    
    
    public static LatticeCard
    getInstance()
    {
        return latticeCard;
    }
    

    /**
     * @param args the command line arguments
     */
    public static void 
    main( String[] argv ) 
    throws InterruptedException
    {
        latticeCard = new LatticeCard();
        latticeCard.start();
    }



    private MenuBar createMenuBar()
    {
        MenuBar _menubar = new MenuBar();
        _menubar.add( createFileMenu() );
        _menubar.add( createEditMenu() );
        _menubar.add( createObjectsMenu() );
        _menubar.add( createGoMenu() );
        _menubar.add( createFormatMenu() );
        _menubar.add( createHomeMenu() );
        return _menubar;
    }
    
    
    private Menu createFileMenu()
    {
        Menu _menu = new Menu( "File" );
        
        _menu.add( "New Stack..." );
        _menu.add( "Open Stack..." );
        _menu.add( "Close Stack" );
        _menu.add( "Save As..." );
        _menu.addSeparator();
        _menu.add( "Compact Stack" );
        _menu.add( "Protect Stack..." );
        _menu.add( "Delete Stack..." );
        _menu.addSeparator();
        _menu.add( "Page Setup..." );
        _menu.add( "Print Field..." );
        _menu.add( "Print Card" );
        _menu.add( "Print Stack..." );
        _menu.add( "Print Report..." );
        
        return _menu;
    }
    
    
    private Menu createObjectsMenu()
    {
        Menu _menu = new Menu( "Objects" );
        
        _menu.add( "Button Info..." );
        _menu.add( "Field Info..." );
        _menu.add( "Card Info..." );
        _menu.add( "Background Info..." );
        _menu.add( "Stack Info..." );
        _menu.addSeparator();
        _menu.add( "Raise" );
        _menu.add( "Lower" );
        _menu.addSeparator();
        MenuItem _newButton = new MenuItem( "New Button" );
        _newButton.addActionListener( new NewButtonHandler() );
        _menu.add( _newButton );
        _menu.add( "New Field" );
        _menu.add( "New Background" );
        
        return _menu;
    }
    
    
    public void start()
    {    
        window.setVisible( true );
        
        toolPalette.setVisible( true );
    }

    
    private Menu createEditMenu()
    {
        Menu _menu = new Menu( "Edit" );
        
        _menu.add( "Undo" );
        _menu.addSeparator();
        _menu.add( "Cut" );
        _menu.add( "Copy" );
        _menu.add( "Paste" );
        _menu.add( "Clear" );
        _menu.addSeparator();
        _menu.add( "New Card" );
        _menu.add( "Delete Card" );
        _menu.add( "Cut Card" );
        _menu.add( "Copy Card" );
        _menu.addSeparator();
        _menu.add( "Text Style..." );
        _menu.add( "Background" );
        _menu.add( "Icon..." );
        _menu.addSeparator();
        _menu.add( "Audio..." );

        return _menu;
    }

    private Menu createGoMenu()
    {
        Menu _menu = new Menu( "Go" );
                
        _menu.add( "Back" );
        _menu.add( "Home" );
        _menu.add( "Recent" );
        _menu.addSeparator();
        _menu.add( "First" );
        _menu.add( "Previous" );
        _menu.add( "Next" );
        _menu.add( "Last" );
        _menu.addSeparator();
        _menu.add( "Find..." );
        _menu.add( "Message" );
        _menu.add( "Scroll" );

        return _menu;
    }

    private Menu createFormatMenu()
    {
        Menu _menu = new Menu( "Format" );
                
        _menu.add( "Font..." );
        _menu.add( "Larger" );
        _menu.add( "Smaller" );
        _menu.addSeparator();
        _menu.add( "Plain" );
        _menu.add( "Bold" );
        _menu.add( "Italic" );
        _menu.add( "Underline" );
        _menu.add( "Outline" );
        _menu.add( "Shadow" );
        _menu.add( "Condense" );
        _menu.add( "Extend" );
        _menu.add( "Group" );

        return _menu;
    }


    private Menu createHomeMenu()
    {
        Menu _menu = new Menu( "Home" );
                
        _menu.add( "Preferences..." );

        return _menu;
    }


    public Stack getStack()
    {
        return stack;
    }
    
    
    class LatticeWindowListener 
    implements WindowListener
    {

        public LatticeWindowListener()
        {
        }


        @Override
        public void windowOpened(WindowEvent e)
        {
            
        }


        @Override
        public void windowClosing(WindowEvent e)
        {
            
        }


        @Override
        public void windowClosed(WindowEvent e)
        {
            
        }


        @Override
        public void windowIconified(WindowEvent e)
        {
            LatticeCard.this.toolPalette.setVisible( false );
        }


        @Override
        public void windowDeiconified(WindowEvent e)
        {
            LatticeCard.this.toolPalette.setVisible( true );
        }


        @Override
        public void windowActivated(WindowEvent e)
        {
            //LatticeCard.this.toolPalette.setVisible( true );
        }


        @Override
        public void windowDeactivated(WindowEvent e)
        {
            //LatticeCard.this.toolPalette.setVisible( false );
        }
    }

    
    class NewButtonHandler
    implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            LatticeCard.this.stack.getCurrentCard().addThing( ObjectFactory.getInstance().newButton() );
        }
        
    }


    public ToolPalette getToolPalette()
    {
        return toolPalette;
    }


    public void setToolPalette(ToolPalette toolPalette)
    {
        this.toolPalette = toolPalette;
    }


    public Frame getWindow()
    {
        return window;
    }


    public void setWindow(Frame window)
    {
        this.window = window;
    }


    public AskDialog getAskDialog()
    {
        return askDialog;
    }


    public void setAskDialog(AskDialog askDialog)
    {
        this.askDialog = askDialog;
    }


    public AnswerDialog getAnswerDialog()
    {
        return answerDialog;
    }


    public void setAnswerDialog(AnswerDialog answerDialog)
    {
        this.answerDialog = answerDialog;
    }
    
    
}
